package a12_图论;

import java.util.Scanner;

/**
 * <p>
 * a07_岛屿的最大面积复习2
 * </p>
 *
 * @author flyduck
 * @since 2025/4/8
 */
public class a07_岛屿的最大面积复习2 {
    public static void main(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        int rows = scanner.nextInt();
//        int cols = scanner.nextInt();
//
//        int[][] grid = new int[rows][cols];
//        for (int i = 0; i < rows; i++) {
//            for (int j = 0; j < cols; j++) {
//                grid[i][j] = scanner.nextInt();
//            }
//        }

        Scanner scanner = new Scanner(System.in);
        int rows = 4;
        int cols = 5;

        int[][] grid = new int[][]{
            {1,1,0,0,0},
            {1,1,0,0,0},
            {0,0,1,0,0},
            {0,0,0,1,1}
        };

        int maxArea = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if(grid[i][j] == 1){
                    area = 0;
                    area++;
                    grid[i][j] = 0;
                    dfs(grid,i,j);
                    maxArea = Math.max(maxArea, area);
                }
            }
        }
        System.out.println(maxArea);
    }

    public static int area;

    public static final int[][] dirs = new int[][]{{0,1},{1,0},{-1,0},{0,-1}};

    private static void dfs(int[][] grid, int x, int y) {
        for (int[] dir : dirs) {
            int nextX = x + dir[0];
            int nextY = y + dir[1];
            if(nextX >= 0 && nextX < grid.length && nextY >= 0 && nextY < grid[0].length){
                if(grid[nextX][nextY] == 1){
                    area++;
                    grid[nextX][nextY] = 0;
                    dfs(grid,nextX,nextY);
                }
            }
        }
    }
}
